package com.aaa.exception;

import com.aaa.entity.ResultCode;
import com.aaa.entity.ResultMsg;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.security.oauth2.common.exceptions.UnsupportedGrantTypeException;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;

public class OauthResponseExceptionTranslator implements WebResponseExceptionTranslator {
    @Override
    public ResponseEntity translate(Exception e) throws Exception {
        ResultMsg resultMsg = null;
        if(e instanceof UnsupportedGrantTypeException)
        {
            resultMsg = ResultMsg.error(ResultCode.UNSUPPORTED_GRANT_TYPE.getCode(),
                    ResultCode.UNSUPPORTED_GRANT_TYPE.getMsg());
        }else if(e instanceof InvalidGrantException)
        {
            resultMsg = ResultMsg.error(ResultCode.BAD_CREDENTIALS.getCode(),
                    ResultCode.BAD_CREDENTIALS.getMsg());
        }else if(e instanceof InternalAuthenticationServiceException)
        {
            resultMsg = ResultMsg.error(ResultCode.NO_DISABLED.getCode(),
                    ResultCode.NO_DISABLED.getMsg());
        }
        else{
            resultMsg = ResultMsg.error(ResultCode.ERROR.getCode(),
                    ResultCode.ERROR.getMsg());
        }
        return new ResponseEntity<>(resultMsg, HttpStatus.OK);
    }
}
